package com.itheima.reggie.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 公共工具拷贝类
 *
 * @author Upjqs
 * @since 2024-02-06 2:11
 */
@Slf4j
public class BeanUtil {
    //private static final Logger log = LoggerFactory.getLogger(BeanUtil.class);

    public BeanUtil() {
    }

    public static <T> T cloneBean(Object source, Class<T> clazz) {
        log.info("=====[BeanUtil.cloneBean]拷贝属性，Param[class:{}，source{}]=====", clazz.getSimpleName(), source);
        if (source == null) {
            return null;
        } else {
            try {
                T t = clazz.newInstance();
                org.springframework.beans.BeanUtils.copyProperties(source, t);
                return t;
            } catch (Exception var3) {
                log.error("=====[BeanUtil.cloneBean]拷贝属性异常，Param[class:{}，content{}]、Error[{}]=====", new Object[]{clazz.getSimpleName(), source, var3});
                // throw new BusinessException(DefaultErrorConstant.SYSTEM_BEAN_COPY_FAILED, var3);
                throw new RuntimeException(var3.getMessage());
            }
        }
    }

    public static Object copyBean(Object source, Object target) {
        log.info("=====[BeanUtil.copyBean]拷贝属性，Param[source{}，target{}]=====", source, target);
        if (target == null) {
            return null;
        } else if (source == null) {
            return target;
        } else {
            try {
                org.springframework.beans.BeanUtils.copyProperties(source, target);
                return target;
            } catch (Exception var3) {
                log.error("=====[BeanUtil.copyBean]拷贝属性异常，Param[source{}，target{}]、Error[{}]=====", new Object[]{source, target, var3});
                //throw new BusinessException(DefaultErrorConstant.SYSTEM_BEAN_COPY_FAILED, var3);
                throw new RuntimeException(var3.getMessage());
            }
        }
    }

}
